package other.up.monitor;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author bingo
 * @Description
 * @Date 2018/8/7
 */
public class LineFrame extends JFrame {


    private int canvasWeight;

    private int canvasHeight;

    public  LineFrame(String title,int canvasWeight,int canvasHeight){
        super(title);
        this.canvasHeight = canvasHeight;
        this.canvasWeight = canvasWeight;

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        LineCanvas lineCanvas = new LineCanvas();
        setContentPane(lineCanvas);
        this.pack();
        setResizable(false);
    }
    private   Map<Integer, Integer> data ;

    public void render( Map<Integer, Integer> data ){
        this.data = data;
        repaint();
    }

    public LineFrame(String title)  {
        this(title,1208,768);
    }

    private class LineCanvas extends  JPanel{

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2D = (Graphics2D) g;
            AlgoHelpler.setStrokeWeight(g2D, 3);

            g2D.drawLine(Const.xOffset, Const.verticalLength + Const.yOffset, Const.horizontalLength + Const.xOffset, Const.verticalLength + Const.yOffset);

            g2D.drawLine(Const.xOffset, Const.yOffset, Const.xOffset, Const.verticalLength + Const.yOffset);

            int n = 12;
            int xLevel = Const.horizontalLength / n;
            for (int i = 0; i <= n; i++) {
                g2D.drawLine(Const.xOffset + (i * xLevel), Const.verticalLength + Const.yOffset, Const.xOffset + (i * xLevel), Const.verticalLength - 10 + Const.yOffset);
                if (i != 0) {
                    g2D.drawString(String.valueOf(i) + "月", Const.xOffset + (i * xLevel), Const.verticalLength - 10 + Const.yOffset);
                }

            }
            int k = 10;
            int yLevel = Const.verticalLength / 10;
            for (int i = 0; i < 10; i++) {
                g2D.drawLine(Const.xOffset, Const.yOffset + i * yLevel, Const.xOffset + 10, Const.yOffset + i * yLevel);
                g2D.drawString(String.valueOf(Const.verticalLength - yLevel * i), Const.xOffset + 10, Const.yOffset + i * yLevel);

            }
            Set<Integer> months = data.keySet();
            Map<Integer,Point> points = new HashMap<>();
            for(Integer month:months){
                int x =  month*xLevel+Const.xOffset;
                int y = Const.verticalLength-data.get(month)+Const.yOffset;
                Point point = new Point(x,y,data.get(month));
                points.put(month,point);
            }
            AlgoHelpler.setColor(g2D,Color.orange);
          for(int i=1;i<12;i++){
                g2D.drawLine(points.get(i).getX(),points.get(i).getY(),points.get(i+1).getX(),points.get(i+1).getY());
                g2D.drawString(String.valueOf(points.get(i).getData()),points.get(i).getX(),points.get(i).getY()+10);
          }

        }


        @Override
        public Dimension getPreferredSize() {
            return  new Dimension(canvasWeight,canvasHeight);
        }

    }

}
